//
//  BYPhotoSelectorCell.m
//  School+
//
//  Created by Sean Sebastian on 5/16/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYPhotoSelectorCell.h"

@interface BYPhotoSelectorCell ()

@property (nonatomic , weak) UIButton *addButton;
@property (nonatomic , weak) UIButton *removeButton;

@end

@implementation BYPhotoSelectorCell

- (void)setImage:(UIImage *)image
{
    [self.addButton setImage:image forState:UIControlStateNormal];
    [self.addButton setImage:image forState:UIControlStateHighlighted];
    self.removeButton.hidden = NO;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self prepareUI];
    return self;
}

- (void)prepareUI
{
    [self.contentView addSubview:self.addButton];
    [self.contentView addSubview:self.removeButton];
    
    self.addButton.translatesAutoresizingMaskIntoConstraints = false;
    self.removeButton.translatesAutoresizingMaskIntoConstraints = false;
    
    NSDictionary *views = @{@"ab" : self.addButton , @"rb" : self.removeButton};
    
    // 添加约束
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[ab]-0-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[ab]-0-|" options:0 metrics:nil views:views]];
    
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[rb]-0-|" options:0 metrics:nil views:views]];
    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[rb]"options:0 metrics:nil views:views]];
    
}

- (void)setAddButton
{
    // 设置按钮图片
    [self.addButton setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
    [self.addButton setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
    
    // 隐藏删除按钮
    self.removeButton.hidden = YES;
    
}


- (void)addPhoto
{
    [self.cellDelegate photoSelectorCellAddPhoto:self];
}

- (void)removePhoto
{
    [self.cellDelegate photoSelectorCellRemovePhoto:self];
}

- (UIButton *)addButton
{
    if (_addButton == nil) {
        UIButton *btn = [[UIButton alloc] init];
        
        [btn setImage:[UIImage imageNamed:@"compose_pic_add"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"compose_pic_add_highlighted"] forState:UIControlStateHighlighted];
        // 图片的显示模式
        [btn.imageView setContentMode:UIViewContentModeScaleAspectFit];
        
        [btn addTarget:self action:@selector(addPhoto) forControlEvents:UIControlEventTouchUpInside];
        _addButton = btn;
        return btn;
    }
    return _addButton;
}

- (UIButton *)removeButton
{
    if (_removeButton == nil) {
        UIButton *btn = [[UIButton alloc] init];
        [btn setImage:[UIImage imageNamed:@"compose_photo_close"] forState:UIControlStateNormal
         ];
        [btn addTarget:self action:@selector(removePhoto) forControlEvents:UIControlEventTouchUpInside];
        _removeButton = btn;
        return btn;
    }
    return _removeButton;
}

@end
